package com.sdg.generatedata;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

public class ProducterData {
    public static void main(String[] args) throws ParseException {
        int n = 40018; // 假设我们要生成10条数据
        List<String> taxiDataList = generateTaxiData(n);
        writeDataToFile(taxiDataList, ".\\bigdataAnalysis\\src\\main\\resources\\taxi_data.csv"); // 将数据写入文件
    }

    private static void writeDataToFile(List<String> taxiDataList, String fileName) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName))) {
            writer.write("rec_date,rec_carid,take_memony,provinces,distance,rec_speed");
            writer.newLine();
            for (String data : taxiDataList) {
                writer.write(data);
                writer.newLine(); // 添加新行以分隔每条记录
            }
            System.out.println("数据已成功写入文件：" + fileName);
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("写入文件时发生错误");
        }
    }

    public static List<String> generateTaxiData(int n) throws ParseException {
        List<String> dataList = new ArrayList<>();
        Random random = new Random();
        String[] provinces = {"北京", "上海", "广东", "江苏", "浙江", "四川", "山东", "河南", "湖北", "湖南", "河北", "山西", "辽宁", "吉林", "黑龙江", "安徽", "福建", "江西", "重庆", "天津", "陕西", "甘肃", "青海", "云南", "贵州", "海南", "内蒙古", "广西", "西藏", "宁夏", "新疆", "台湾", "香港", "澳门"};

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date startDate = sdf.parse("2016-01-01");
        Date endDate = sdf.parse("2024-07-01");

        for (int i = 0; i < n; i++) {
            long rideDate = startDate.getTime() + (long) (random.nextDouble() * (endDate.getTime() - startDate.getTime()));
            String date = sdf.format(new Date(rideDate));

            int carId = random.nextInt(100000) + 1; // 假设车辆ID是1到100000之间的整数
            double fare = 10 + random.nextDouble() * 190; // 乘车费用在10元到200元之间
            String province = provinces[random.nextInt(provinces.length)]; // 随机选择一个省份
            int distance = 10 + random.nextInt(291); // 乘车距离在10和300之间
            int speed = 5 + random.nextInt(100); // 假设记录速度在5到104之间（简单示例，实际情况可能更复杂）

            String data = String.format("%s, %d, %.2f, %s, %d, %d", date, carId, fare, province, distance, speed);
            dataList.add(data);
        }

        return dataList;
    }
}
